package com.edu.controller;

import com.edu.dao.UserMapper;
import com.edu.data.User;
import com.edu.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@Controller
@RequestMapping("/login")
public class LoginController {
    SqlSession sqlSession= MyBatisUtil.createSession();
    UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

    @RequestMapping("/toLogin")
    private String toLogin(){
        return "login";
    }

    @RequestMapping("/log")
    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name=req.getParameter("userId");
        String pass=req.getParameter("password");
        String incode=req.getParameter("code");
        String rightcode= (String) req.getSession().getAttribute("numrand");
        String action = req.getParameter("action");
        if (!incode.equals(rightcode)){
            System.out.println("验证码错误！");
            resp.sendRedirect("toLogin");
        }else {
            try {
                User user = userMapper.findById(name);
                if (user!=null){
                    if (user.getEuPassword().equals(pass)){
                        Cookie nameCookie=new Cookie("usercode",name);
                        Cookie passCookie=new Cookie("pass",pass);
                        nameCookie.setMaxAge(7*24*60*60);
                        passCookie.setMaxAge(7*24*60*60);
                        resp.addCookie(nameCookie);
                        resp.addCookie(passCookie);
                        System.out.println("密码正确");
                        req.getSession().setAttribute("loginUser",user);
                        req.getRequestDispatcher("../home/index").forward(req,resp);
                    }else {
                        Cookie nameCookie=new Cookie("usercode","");
                        Cookie passCookie=new Cookie("pass","");
                        nameCookie.setMaxAge(0);
                        passCookie.setMaxAge(0);
                        System.out.println("密码错误");
                        req.getRequestDispatcher("toLogin").forward(req,resp);
                    }
                }else {
                    System.out.println("用户不存在");
                    req.getRequestDispatcher("toLogin").forward(req,resp);
                }
            } catch (ServletException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/exitLogin")
    private String exitLogin(HttpServletRequest req){
        req.getSession().removeAttribute("loginUser");
        req.getSession().removeAttribute("cart");
        return "redirect:../home/index";
    }

    @RequestMapping("/vcLogin")
    private String vcLogin(){
        return "Number";
    }
}
